package com.bto.solar.work.controller;

import com.bto.solar.work.common.R;
import com.bto.solar.work.dao.ILeaseBankDicService;
import com.bto.solar.work.entity.LeaseBankDic;
import com.bto.solar.work.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author adrian
 * @since 2023-09-26
 */
@RestController
@RequestMapping("/lease-bank")
public class LeaseBankDicController {
    @Autowired
    private ILeaseBankDicService leaseBankDicService;
    /**
     * 查询越秀银行信息
     */
    @GetMapping("/sub-bank")
    public R leaseSubBankInfo(@RequestParam(required = false) String bankName ,
                              @RequestParam(required = false) String queryTag){
        List<LeaseBankDic> bankDics ;
        if(StringUtils.isEmpty(queryTag)){
            bankDics = leaseBankDicService.lambdaQuery()
                    .eq(StringUtils.isNotEmpty(bankName) , LeaseBankDic::getBankName , bankName)
                    .last("LIMIT 100")
                    .list();
        }
        else {
            bankDics = leaseBankDicService.lambdaQuery()
                    .eq(StringUtils.isNotEmpty(bankName) , LeaseBankDic::getBankName , bankName)
                    .like(LeaseBankDic::getBankName, queryTag).or()
                    .like(LeaseBankDic::getBankFullName, queryTag).or()
                    .like(LeaseBankDic::getProvinceName, queryTag).or()
                    .like(LeaseBankDic::getCityName, queryTag)
                    .last("LIMIT 100")
                    .list();
        }
        return R.ok(bankDics);
    }

    /**
     * 查询越秀银行信息
     */
    @GetMapping("/bank")
    public R leaseBankInfo(@RequestParam(required = false) String queryTag){
        Set<LeaseBankDic> bankDics = new HashSet<>(leaseBankDicService.lambdaQuery()
                .select(LeaseBankDic::getBankName)
                .isNotNull(LeaseBankDic::getBankName)
                .like(StringUtils.isNotEmpty(queryTag) , LeaseBankDic::getBankName, queryTag)
                .groupBy(LeaseBankDic::getBankName)
                .list());
        return R.ok(bankDics.stream().map(LeaseBankDic::getBankName).collect(Collectors.toSet()));
    }
}
